package com.elvt.server.controller;


import com.elvt.server.mapper.AdminMapper;
import com.elvt.server.pojo.Admin;
import com.elvt.server.pojo.Role;
import com.elvt.server.service.IAdminService;
import com.elvt.server.service.IRoleService;
import com.elvt.server.utils.RespSult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者：elvt
 * @since 2022-05-21
 */
@RestController
@RequestMapping("system/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "获取所有的操作员")
    @GetMapping
    public List<Admin> findAllAdmin(String keywords){
        return adminService.findAllAdminByKeywords(keywords);

    }

    @ApiOperation(value = "修改操作员")
    @PutMapping
    public RespSult update(@RequestBody Admin admin){

        if (adminService.updateById(admin)){
            return RespSult.success("修改成功!");
        }
        return RespSult.error("修改失败！");
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/{id}")
    public RespSult deleteById(@PathVariable Integer id){
        if (adminService.removeById(id)){
            return RespSult.success("删除成功!");
        }
        return RespSult.error("删除失败！");
    }
    @ApiOperation(value = "新增操作员")
    @PostMapping
    public RespSult save(@RequestBody Admin admin){
        if (adminService.save(admin)){

            return RespSult.success("添加成功！");

        }
        return RespSult.error("添加失败!");
    }
    @ApiOperation(value = "获取所以角色")
    @GetMapping("/roles")
    public List<Role> getAllRole(){

       return roleService.list();
    }
    @ApiOperation(value = "更新操作员角色")
    @PutMapping("/role")
    public RespSult updateAdminRole(Integer adminId,Integer[] rids){
        return adminService.updateAdminRole(adminId,rids);
    }


}
